home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.05 May 90 / Line Art Rotation Source / cubemain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-06  |  6.0 KB  |  177 lines  |  [TEXT/KAHL]

  1. #include<math.h>
  2. /* Following is inline macro for drawing lines    */
  3. #define viewpts(s) {for(i=0;i<numoflns;i++)         \
  4.                      { MoveTo((int)vertex[s][line[i].v1].x,     \
  5.                        (int)vertex[s][line[i].v1].y);     \
  6.                        LineTo((int)vertex[s][line[i].v2].x,     \
  7.                        (int)vertex[s][line[i].v2].y); }}  
  8.  
  9. #define numofpts 8    /* A cube has eight vertices    */
  10. #define numoflns 12           /* lines for every face.    */
  11.  
  12. /* the following are the data structs for vertices and lines*/    
  13. typedef struct rec1 {float x,y,z;} point3d;
  14. typedef struct rec2 {int v1,v2;} edge;
  15.  
  16. void mult();        /* Matrices multiplication    */
  17.  
  18. main()
  19. {
  20.   point3d vertex[2][8],    /* array of 3D pts    */
  21.           center;    /* centroid of cube    */
  22.   edge    line[12];    /* array of lines    */
  23.   int     buttondown=0,    /* mousedwn flag(for prog end)*/
  24.           keypressed=0,       /* keydwn flg(for arrows)     */
  25.           flip=0,             /* This is index for vertex so*/
  26.           flop=1,             /* can undraw flip & draw flop*/
  27.           i,                  /* counter        */
  28.           rot=0;    /* Flag for direction of rotat*/
  29.   long    low;        /* low word of keydwn message    */
  30.   float   a,        /* Particular angle of rotat    */
  31.           R[4][4],    /* Rotation matrix    */
  32.           c[4][4],    /* Product of trans & rot mats*/
  33.           d[4][4],    /* Product of c and inv trans */
  34.           T[4][4],Tinv[4][4], /* Translation & inv trans    */
  35.           x=0.087266;    /* Algle of rot in rad    */
  36.   EventRecord nextevent;
  37.   KeyMap    thekeys;
  38.   WindowPtr scnwdw;
  39.   Rect      scnrect;
  40. /*********************************************
  41. *  Set things up        *
  42. *********************************************/
  43. InitGraf(&thePort);
  44. InitFonts();
  45. FlushEvents(everyEvent,0);
  46. InitWindows();
  47. InitMenus();
  48. TEInit();
  49. InitDialogs(0);
  50. InitCursor();
  51. scnrect=screenBits.bounds;
  52. InsetRect(&scnrect,50,50);
  53. scnwdw=NewWindow(0,&scnrect,"\p",TRUE,dBoxProc,-1,FALSE,0);
  54.   
  55. /*********************************************
  56. *  Get points. Arbitrary cube.    *
  57. *********************************************/
  58. center.x=300;center.y=200;center.z=120;
  59. vertex[0][0].x=280;vertex[0][0].y=220;vertex[0][0].z=100;
  60. vertex[0][1].x=320;vertex[0][1].y=220;vertex[0][1].z=100;
  61. vertex[0][2].x=320;vertex[0][2].y=180;vertex[0][2].z=100;
  62. vertex[0][3].x=280;vertex[0][3].y=180;vertex[0][3].z=100;
  63. vertex[0][4].x=280;vertex[0][4].y=220;vertex[0][4].z=140;
  64. vertex[0][5].x=320;vertex[0][5].y=220;vertex[0][5].z=140;
  65. vertex[0][6].x=320;vertex[0][6].y=180;vertex[0][6].z=140;
  66. vertex[0][7].x=280;vertex[0][7].y=180;vertex[0][7].z=140;
  67. line[0].v1=0;line[0].v2=1;
  68. line[1].v1=1;line[1].v2=2;
  69. line[2].v1=2;line[2].v2=3;
  70. line[3].v1=3;line[3].v2=0;
  71. line[4].v1=0;line[4].v2=4;
  72. line[5].v1=1;line[5].v2=5;
  73. line[6].v1=2;line[6].v2=6;
  74. line[7].v1=3;line[7].v2=7;
  75. line[8].v1=4;line[8].v2=5;
  76. line[9].v1=5;line[9].v2=6;
  77. line[10].v1=6;line[10].v2=7;
  78. line[11].v1=7;line[11].v2=4;
  79. T[0][0]=1;T[0][1]=0;T[0][2]=0;T[0][3]=0;
  80. T[1][0]=0;T[1][1]=1;T[1][2]=0;T[1][3]=0;
  81. T[2][0]=0;T[2][1]=0;T[2][2]=1;T[2][3]=0;
  82. T[3][0]=-center.x;T[3][1]=-center.y;T[3][2]=-center.z;T[3][3]=1;
  83. Tinv[0][0]=1;Tinv[0][1]=0;Tinv[0][2]=0;Tinv[0][3]=0;
  84. Tinv[1][0]=0;Tinv[1][1]=1;Tinv[1][2]=0;Tinv[1][3]=0;
  85. Tinv[2][0]=0;Tinv[2][1]=0;Tinv[2][2]=1;Tinv[2][3]=0;
  86. Tinv[3][0]=center.x;Tinv[3][1]=center.y;Tinv[3][2]=center.z;Tinv[3][3]=1;
  87.  
  88. /*********************************************
  89. *  Rotate            *
  90. *********************************************/
  91. viewpts(flip);        /* This draws first set of pts*/
  92.   while(!buttondown)    /* Mini event loop    */
  93.   {
  94.     keypressed=0;
  95.     SystemTask();
  96.     if(GetNextEvent(-1,&nextevent))
  97.       if(nextevent.what==mouseDown) buttondown=1;
  98.       else if(nextevent.what==keyDown) keypressed=1;
  99.       else if(nextevent.what==autoKey) keypressed=1;
  100.     if(keypressed)     /* Find out which one    */
  101.     {
  102.       keypressed=0;
  103.       low=LoWord(nextevent.message);
  104.       low=BitShift(low,-8);
  105.       if(low==126) {rot=1;a=-x;} /* Set dir flag and-*/
  106.       if(low==124) {rot=2;a=-x;} /* angle(pos or neg */
  107.       if(low==125) {rot=3;a=x;}
  108.       if(low==123) {rot=4;a=x;}
  109.       switch(rot)
  110.       {
  111.         case 1:/* Both of these are rot about the X axis    */
  112.         case 3: R[0][0]=1;R[0][1]=0;R[0][2]=0;R[0][3]=0;
  113.                 R[1][0]=0;R[1][1]=cos(a);R[1][2]=sin(a);R[1][3]=0;
  114.                 R[2][0]=0;R[2][1]=-sin(a);R[2][2]=cos(a);R[2][3]=0;
  115.                 R[3][0]=0;R[3][1]=0;R[3][2]=0;R[3][3]=1;break;
  116.         case 2:/* Both of these are rot about the Y axis    */
  117.         case 4: R[0][0]=cos(a);R[0][1]=0;R[0][2]=-sin(a);R[0][3]=0;
  118.                 R[1][0]=0;R[1][1]=1;R[1][2]=0;R[1][3]=0;
  119.                 R[2][0]=sin(a);R[2][1]=0;R[2][2]=cos(a);R[2][3]=0;
  120.                 R[3][0]=0;R[3][1]=0;R[3][2]=0;R[3][3]=1;break;
  121.       }  /*end switch*/
  122.       mult(T,R,c);    /* Combine trans & rotation    */
  123.       mult(c,Tinv,d);    /* Combine that and inv trans */
  124.       flip++;flip=flip%2;flop++;flop=flop%2; /* flip flop   */
  125.       /* The following actually calculates new vert of rotat*/
  126.       for(i=0;i<numofpts;i++)
  127.       {
  128.         vertex[flip][i].x=vertex[flop][i].x*d[0][0]
  129.                     +vertex[flop][i].y*d[1][0]
  130.                     +vertex[flop][i].z*d[2][0]
  131.                     +1*d[3][0];
  132.         vertex[flip][i].y=vertex[flop][i].x*d[0][1]
  133.                     +vertex[flop][i].y*d[1][1]
  134.                     +vertex[flop][i].z*d[2][1]
  135.                     +1*d[3][1];
  136.         vertex[flip][i].z=vertex[flop][i].x*d[0][2]
  137.                     +vertex[flop][i].y*d[1][2]
  138.                     +vertex[flop][i].z*d[2][2]
  139.                     +1*d[3][2];
  140.        }
  141.        ForeColor(whiteColor);
  142.        viewpts(flop);    /* Undraw    */
  143.        ForeColor(blackColor);
  144.        viewpts(flip);    /* Draw    */
  145.     }  /*end update points*/
  146.  
  147.   }
  148.  
  149. /*********************************************
  150. *  End everything        *
  151. *********************************************/
  152. DisposeWindow(scnwdw);
  153. }  /*program end*/
  154.  
  155.  
  156. void mult(A,B,C)
  157.   float A[][4],B[][4],C[][4];
  158. {
  159.   int i,j,k;
  160.   
  161.   for(i=0;i<=3;i++)
  162.     for(j=0;j<=3;j++)
  163.     {
  164.       C[i][j]=0.0;
  165.       for(k=0;k<=3;k++)
  166.         C[i][j]+=A[i][k]*B[k][j];
  167.     }
  168. }  /*end mult*/
  169.       
  170.       
  171.       
  172.       
  173.       
  174.       
  175.       
  176.       
  177.